package com.wande.dataplatform.controller;

import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.bo.JobBo;
import com.wande.dataplatform.domain.vo.JobInstanceVO;
import com.wande.dataplatform.domain.vo.JobVO;
import com.wande.dataplatform.service.IJobService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作业管理控制器
 *
 * @author wande
 */
@Validated
@RestController
@RequestMapping("/dataplatform/job")
@RequiredArgsConstructor
public class JobController extends BaseController {

    private final IJobService jobService;

    /**
     * 查询作业列表
     */
    @GetMapping("/list")
    public TableDataInfo<JobVO> list(JobBo bo, PageQuery pageQuery) {
        return jobService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询所有作业
     */
    @GetMapping("/listAll")
    public R<List<JobVO>> listAll(JobBo bo) {
        return R.ok(jobService.queryList(bo));
    }

    /**
     * 查询作业详情
     */
    @GetMapping("/{id}")
    public R<JobVO> getInfo(@PathVariable Long id) {
        return R.ok(jobService.queryById(id));
    }

    /**
     * 创建作业
     */
    @Log(title = "作业管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> add(@Valid @RequestBody JobBo bo) {
        return R.ok(jobService.createJob(bo));
    }

    /**
     * 更新作业
     */
    @Log(title = "作业管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Valid @RequestBody JobBo bo) {
        jobService.updateJob(bo);
        return R.ok();
    }

    /**
     * 删除作业
     */
    @Log(title = "作业管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        jobService.deleteJob(id);
        return R.ok();
    }

    /**
     * 启动作业
     */
    @Log(title = "作业管理", businessType = BusinessType.OTHER)
    @PostMapping("/{id}/start")
    public R<Long> start(@PathVariable Long id, @RequestParam(defaultValue = "MANUAL") String triggerType) {
        return R.ok(jobService.startJob(id, triggerType));
    }

    /**
     * 停止作业实例
     */
    @Log(title = "作业管理", businessType = BusinessType.OTHER)
    @PostMapping("/instance/{instanceId}/stop")
    public R<Void> stopInstance(@PathVariable Long instanceId) {
        jobService.stopJobInstance(instanceId);
        return R.ok();
    }

    /**
     * 重跑作业实例
     */
    @Log(title = "作业管理", businessType = BusinessType.OTHER)
    @PostMapping("/instance/{instanceId}/rerun")
    public R<Long> rerunInstance(@PathVariable Long instanceId) {
        return R.ok(jobService.rerunJobInstance(instanceId));
    }

    /**
     * 查询作业实例列表
     */
    @GetMapping("/instance/list")
    public TableDataInfo<JobInstanceVO> listInstances(@RequestParam(required = false) Long jobId, PageQuery pageQuery) {
        return jobService.queryInstanceList(jobId, pageQuery);
    }

    /**
     * 查询作业实例详情
     */
    @GetMapping("/instance/{instanceId}")
    public R<JobInstanceVO> getInstanceInfo(@PathVariable Long instanceId) {
        return R.ok(jobService.queryInstanceById(instanceId));
    }

    /**
     * 同步作业实例状态
     */
    @PostMapping("/instance/{instanceId}/sync")
    public R<Void> syncInstanceStatus(@PathVariable Long instanceId) {
        jobService.syncInstanceStatus(instanceId);
        return R.ok();
    }

    /**
     * 获取作业实例日志
     */
    @GetMapping("/instance/{instanceId}/logs")
    public R<String> getInstanceLogs(@PathVariable Long instanceId) {
        return R.ok(jobService.getInstanceLogs(instanceId));
    }
}
